package fun.abbas.demo240621.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import fun.abbas.demo240621.bean.WeatherVo;

public class WeatherLineView extends View {

    private final String tempTextDemo = "00℃";
    private final Rect tempTextDemoBound = new Rect();
    private final List<WeatherVo> tempList = new ArrayList<>();

    //最高温度
    private int allTempMax = 0;
    //最低温度
    private int allTempMin = 0;

    private final int lineSpaceHeight = 200;
    private final int allSpaceWidthSize = 60;
    private int lineGridHeight = 0;

    private final Paint tempTextPaint = new Paint();
    private final Paint maxTempPaint = new Paint();
    private final Paint minTempPaint = new Paint();
    private final Paint maxTempPointPaint = new Paint();
    private final Paint minTempPointPaint = new Paint();
    private float tempTextHeight = 0f;

    private final Path maxTempPath = new Path();
    private final Path minTempPath = new Path();

    private float[] weatherTempMaxPointers;
    private float[] weatherTempMinPointers;


    public WeatherLineView(Context context) {
        super(context);
        init();
    }

    public WeatherLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WeatherLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public WeatherLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = Math.max((int) ((allSpaceWidthSize + tempTextHeight + allSpaceWidthSize) * 2 + lineSpaceHeight),280);

        //设置高度为height，宽度最大
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), height);
    }

    private void init() {
        tempTextPaint.setTextSize(50);
        tempTextPaint.setColor(Color.parseColor("#FF686868"));
        tempTextPaint.setStrokeWidth(5f);
        tempTextPaint.getTextBounds(tempTextDemo, 0, tempTextDemo.length(), tempTextDemoBound);
        tempTextHeight = tempTextDemoBound.height();
        maxTempPaint.setColor(Color.YELLOW);
        maxTempPaint.setStrokeWidth(4f);
        maxTempPaint.setStyle(Paint.Style.STROKE);
        maxTempPaint.setAntiAlias(true);
        maxTempPaint.setStrokeJoin(Paint.Join.ROUND);
        maxTempPaint.setPathEffect(new CornerPathEffect(64));
        minTempPaint.setColor(Color.BLUE);
        minTempPaint.setStrokeWidth(4f);
        minTempPaint.setStyle(Paint.Style.STROKE);
        minTempPaint.setAntiAlias(true);
        minTempPaint.setStrokeJoin(Paint.Join.ROUND);
        minTempPaint.setPathEffect(new CornerPathEffect(64));
        maxTempPointPaint.setColor(Color.YELLOW);
        maxTempPointPaint.setStrokeWidth(16f);
        minTempPointPaint.setColor(Color.BLUE);
        minTempPointPaint.setStrokeWidth(16f);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        if (tempList.isEmpty()) return;
        maxTempPath.moveTo(0, getTempSpaceYMax());
        minTempPath.moveTo(0, getTempSpaceYMin());
        for (int i = 0; i < tempList.size(); i++) {
            WeatherVo weatherVo = tempList.get(i);
            float tempDrawX = getTempDrawX(i);
            weatherTempMaxPointers[i * 2] = tempDrawX;
            weatherTempMaxPointers[i * 2 + 1] = getTempDrawY(weatherVo.getMaxTemp());
            weatherTempMinPointers[i * 2] = tempDrawX;
            weatherTempMinPointers[i * 2 + 1] = getTempDrawY(weatherVo.getMinTemp());
        }
        for (int i = 0; i < weatherTempMaxPointers.length; i += 2) {
            maxTempPath.lineTo(weatherTempMaxPointers[i], weatherTempMaxPointers[i + 1]);
        }
        for (int i = 0; i < weatherTempMinPointers.length; i += 2) {
            minTempPath.lineTo(weatherTempMinPointers[i], weatherTempMinPointers[i + 1]);
        }
        maxTempPath.lineTo(getWidth(), weatherTempMaxPointers[weatherTempMaxPointers.length-1]);
        minTempPath.lineTo(getWidth(), weatherTempMinPointers[weatherTempMinPointers.length-1]);

        canvas.drawPath(maxTempPath, maxTempPaint);
        canvas.drawPath(minTempPath, minTempPaint);
        for (int i = 0; i < weatherTempMaxPointers.length; i += 2) {
            canvas.drawCircle(weatherTempMaxPointers[i], weatherTempMaxPointers[i + 1], maxTempPointPaint.getStrokeWidth() / 2, maxTempPointPaint);
            String tempStr = "";
            try {
                tempStr = tempList.get(i / 2).getMaxTemp() + "℃";
            } catch (Exception e) {
                tempStr = tempList.get(0).getMaxTemp() + "℃";
            }
            float textWidth = tempTextPaint.measureText(tempStr);
            float textLeft = weatherTempMaxPointers[i] - textWidth / 2;
            float textTop = weatherTempMaxPointers[i + 1] - tempTextPaint.getFontSpacing() / 2;
            canvas.drawText(tempStr, textLeft, textTop, tempTextPaint);
        }
        for (int i = 0; i < weatherTempMinPointers.length; i += 2) {
            canvas.drawCircle(weatherTempMinPointers[i], weatherTempMinPointers[i + 1], minTempPointPaint.getStrokeWidth() / 2, minTempPointPaint);
            String tempStr = "";
            try {
                tempStr = tempList.get(i / 2).getMinTemp() + "℃";
            } catch (Exception e) {
                tempStr = tempList.get(0).getMinTemp() + "℃";
            }
            float textWidth = tempTextPaint.measureText(tempStr);

            float textLeft = weatherTempMinPointers[i] - textWidth / 2;
            float textTop = weatherTempMinPointers[i + 1] + tempTextPaint.getFontSpacing();
            canvas.drawText(tempStr, textLeft, textTop, tempTextPaint);
        }
    }

    private int getTempDrawY(int temp) {
        float paddingTop = allSpaceWidthSize + tempTextHeight + allSpaceWidthSize;
        return (int) ((allTempMax - temp) * lineGridHeight + paddingTop);
    }

    private float getTempDrawX(int index) {
        float tempSpaceWidth = (getWidth() - allSpaceWidthSize * 2) * (index * 1f / (tempList.size() - 1));
        return tempSpaceWidth + allSpaceWidthSize;
    }

    @Deprecated
    private float getTempSpaceYMax() {
        return allSpaceWidthSize + tempTextHeight + allSpaceWidthSize;
    }

    @Deprecated
    private float getTempSpaceYMin() {
        return allSpaceWidthSize + tempTextHeight + allSpaceWidthSize + lineSpaceHeight;
    }

//    private float getTempTextSpaceY(){
//
//    }

    public void setTempList(List<WeatherVo> tempList) {
        maxTempPath.reset();
        minTempPath.reset();
        this.tempList.clear();
        weatherTempMaxPointers = new float[tempList.size() * 2];
        weatherTempMinPointers = new float[tempList.size() * 2];
        this.tempList.addAll(tempList);
        allTempMax = tempList.stream()
                .map(WeatherVo::getMaxTemp)
                .max(Comparator.comparingInt(integer -> integer)).orElse(0);
        allTempMin = tempList.stream()
                .map(WeatherVo::getMinTemp)
                .min(Comparator.comparingInt(i -> i))
                .orElse(0);
        //最大减去最小的差值
        int i = allTempMax - allTempMin;
        lineGridHeight = lineSpaceHeight / i;

        invalidate();
    }
}
